package ae.pcfc.cldos.online.web.controller.client.company.home;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import ae.pcfc.cldos.online.common.delegate.CompanyInformationDelegate;
import ae.pcfc.cldos.online.common.exception.FNPAppException;
import ae.pcfc.cldos.online.common.exception.GSAppException;
import ae.pcfc.cldos.online.common.exception.NoSuchCompanyException;
import ae.pcfc.cldos.online.common.vo.CompanyInformationVO;
import ae.pcfc.cldos.online.common.vo.LabelVO;
import ae.pcfc.cldos.online.common.vo.StatusVO;
import ae.pcfc.cldos.online.common.vo.selectors.SystemValuesVO;
import ae.pcfc.cldos.online.web.Constants;
import ae.pcfc.cldos.online.web.ViewPath;
import ae.pcfc.cldos.online.web.controller.base.BasePageController;
import ae.pcfc.cldos.online.web.model.CompanyInformationModel;

/**
 * Controller that handles all {@link Company}-related requests, i.e.: all
 * requests for /company/register/nonclient/
 * 
 * @author Abdullah
 */
@Controller
@RequestMapping(ViewPath.__HOME)
public class CompanyHomeController extends BasePageController {
	
	private static final String CLIENT_COMPANY_HOME_SHOW = "client.company.home.show";
	
	@Autowired
	@Qualifier("companyInformationDelegate")
	private CompanyInformationDelegate companyInformationDelegate;
	
	
	
	@ModelAttribute(Constants.__MEDICAL_TYPE_MAP_MODEL_KEY)
	public Map<String, String> populateMedicalTypeMap(Locale locale){
		
		List<SystemValuesVO> medicalTypes = systemValuesDelegate.getMedicalType();
		
		Map<String, String> result = new HashMap<String, String>(medicalTypes.size());
		
		for(SystemValuesVO medicalType :medicalTypes ){
			if(locale.getLanguage().equals("ar")){
				result.put(medicalType.getSvCode(), medicalType.getSvDescArb());
			}else {
				result.put(medicalType.getSvCode(), medicalType.getSvDesc());
			}
		}
		
		return result;
	}
	
	@ModelAttribute(Constants.__MEDICAL_TYPE_REQUEST_MODEL_KEY)
	public List<LabelVO> populateMedicalTypeList(Locale locale){

		List<SystemValuesVO> medicalTypes = systemValuesDelegate.getMedicalType();
		
		List<LabelVO> labelList = new ArrayList<LabelVO>(medicalTypes.size()+1);
		LabelVO labelVO = new LabelVO();
		labelList.add(labelVO);
		
		for(SystemValuesVO medicalType :medicalTypes ){
			labelVO = systemValuesDelegate.getMapper().map(medicalType, LabelVO.class);
			labelList.add(labelVO);
		}
		
		return labelList;
	}
	
	/**
	 * @param request
	 * @return
	 */
	@ModelAttribute("StatusList")
	public List<StatusVO> getStatusListAll(HttpServletRequest request){
		List<StatusVO> statusList = adminTaskDelegate.findApplicationStatusByType("ALL");
		List<StatusVO> response = new ArrayList<StatusVO>();
		response.add(new StatusVO());
		response.addAll(statusList);
		return response;
	}
	
	@ModelAttribute("CompanyInformationInfo")
	public CompanyInformationModel populateCompanyInformationInfoModel(HttpServletRequest request){
		CompanyInformationModel companyInformationModel = new CompanyInformationModel();
		CompanyInformationVO companyInformationVO=null;
		try{
			companyInformationVO = companyInformationDelegate.retriveGSInformation(getUserVO());
			mapper.map(companyInformationVO, companyInformationModel);
			companyInformationModel.setCompanyNameAr(companyInformationVO.getCompanyNameAr());
		}catch(NoSuchCompanyException e){
			companyInformationModel.setCompanyError(true);
			e.printStackTrace();
		}catch(GSAppException e){
			companyInformationModel.setGsError(true);
		}
		try{
			companyInformationVO = companyInformationDelegate.retriveFNPInformation(getUserVO());
			companyInformationModel.setPrepaidAccBalance(new Double(companyInformationVO.getPrepaidAccBalance()).toString());
			companyInformationModel.setReservedAccBalance(new Double(companyInformationVO.getReservedAccBalance()).toString());
		}catch(NoSuchCompanyException e){
			companyInformationModel.setCompanyError(true);
		}catch(FNPAppException e){
			companyInformationModel.setFnpError(true);
		}
		return companyInformationModel;
	}
	
	@ModelAttribute
	public void populateBreadCrumbs(ModelMap model) {
		breadCrumbsFactoryService.populateMainPageBreadCrumb(model);
	}

	@RequestMapping(method = RequestMethod.GET, value = "/show.htm")
	public ModelAndView form(
			ModelMap model,			
			@ModelAttribute(value="CompanyInformationModel") CompanyInformationModel companyInformationModel){
		
		ModelAndView mav;
		mav =  new ModelAndView(CLIENT_COMPANY_HOME_SHOW,model);
		return mav;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/form.htm")
	public String postForm(
			HttpServletRequest request,
			SessionStatus status,
			BindingResult errors) throws Exception {
			
		return new String("");
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/show.htm")
	public ModelAndView postShow(
			HttpServletRequest request,
			SessionStatus status,
			@RequestParam(required=false) String medicalType,	
			@ModelAttribute(value="CompanyInformationModel") @Valid CompanyInformationModel companyInformationModel,			
			BindingResult errors) throws Exception {
		
		companyInformationDelegate.setMedicalType(getUserVO(),companyInformationModel.getMedicalType());
			
		ModelAndView mav =  new ModelAndView(CLIENT_COMPANY_HOME_SHOW);
		mav.addObject("medicalType", medicalType);
		
		return mav;
	}
	
}
